package pl.gettoknowme.mobile.android.network.request.impl;

import java.util.List;
import java.util.Map;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;

import flexjson.JSONSerializer;

import pl.gettoknowme.mobile.android.network.request.HttpRequestWrapper;
import pl.gettoknowme.mobile.android.services.sms.SmsNumberStats;
import pl.gettoknowme.mobile.android.services.sms.SmsSingleWordStats;
import pl.gettoknowme.mobile.android.util.Constants;
import pl.gettoknowme.mobile.android.util.LoggingUtil;

/**
 * Wrapper żądania dostarczającego listę zebranych statystyk SMS-ów
 * @author Michał Kołodziejski
 */
public class SmsStatsHttpRequestWrapper implements HttpRequestWrapper {

    /**
     * Dane statystyk SMS-ów
     */
    public class SmsStatsDataObject {
        protected Map<String, SmsSingleWordStats> smsStats;
        protected List<SmsNumberStats> smsNumberStats;

        public Map<String, SmsSingleWordStats> getSmsStats() {
            return smsStats;
        }

        public void setSmsStats(Map<String, SmsSingleWordStats> smsStats) {
            this.smsStats = smsStats;
        }

        public List<SmsNumberStats> getSmsNumberStats() {
            return smsNumberStats;
        }

        public void setSmsNumberStats(List<SmsNumberStats> smsNumberStats) {
            this.smsNumberStats = smsNumberStats;
        }
    }
    
    /**
     * Identyfikator użytkownika
     */
    private String userId;
    /**
     * Lista statystyk SMS-ów do przesłania na serwer
     */
    private Map<String, SmsSingleWordStats> smsStatsMap;
    /**
     * Lista statystyk ilości SMS-ów wymienianych z numerami telefonów
     */
    private List<SmsNumberStats> smsNumberStats;
    
    /**
     * Tworzy wrapper żądania dostarczającego listę zebranych statystyk SMS-ów
     * @param locationList lista zebranych statystyk SMS-ów do przesłania na serwer
     */
    public SmsStatsHttpRequestWrapper(String userId, Map<String, SmsSingleWordStats> smsStatsMap, 
            List<SmsNumberStats> smsNumberStats) {
        this.userId = userId;
        this.smsStatsMap = smsStatsMap;
        this.smsNumberStats = smsNumberStats;
    }
    
    
    @Override
    public HttpUriRequest getRequest() {
        String uri = Constants.SERVER_HOST + "/" + Constants.API_APP_ID + "/sms_stats";
        HttpPost httpPost = new HttpPost(uri);
        httpPost.setEntity(HttpRequestUtil.createHttpEntity(serializeData()));
        return httpPost;
    }

    protected String serializeData(){
        SmsStatsDataObject dataObject = new SmsStatsDataObject();
        dataObject.setSmsStats(smsStatsMap);
        dataObject.setSmsNumberStats(smsNumberStats);
        APIData apiData = new APIData(userId, dataObject);
        
        JSONSerializer serializer = new JSONSerializer();
        String serialized = serializer
                .exclude("*.class")
                .deepSerialize(apiData);
        LoggingUtil.d(this, "Serialized data: "+serialized);
        return serialized;
    }
}
